package org.dromara.system.controller.coreController;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.AppUserVo;
import org.dromara.system.domain.bo.AppUserBo;
import org.dromara.system.service.IAppUserService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * APP用户信息
 *
 * @author Lion Li
 * @date 2025-09-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/user")
public class AppUserController extends BaseController {

    private final IAppUserService appUserService;

    /**
     * 查询APP用户信息列表
     */
    @SaCheckPermission("app:user:list")
    @GetMapping("/list")
    public TableDataInfo<AppUserVo> list(AppUserBo bo, PageQuery pageQuery) {
        return appUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出APP用户信息列表
     */
    @SaCheckPermission("app:user:export")
    @Log(title = "APP用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppUserBo bo, HttpServletResponse response) {
        List<AppUserVo> list = appUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "APP用户信息", AppUserVo.class, response);
    }

    /**
     * 获取APP用户信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("app:user:query")
    @GetMapping("/detail/{id}")
    public R<AppUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        if (id.equals(0L)){
            id = getLoginUser().getUserId();
        }
        return R.ok(appUserService.queryById(id));
    }

    /**
     * 新增APP用户信息
     */
    @SaCheckPermission("app:user:add")
    @Log(title = "APP用户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppUserBo bo) {
        return toAjax(appUserService.insertByBo(bo));
    }

    /**
     * 修改APP用户信息
     */
    @SaCheckPermission("app:user:edit")
    @Log(title = "APP用户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppUserBo bo) {
        return toAjax(appUserService.updateByBo(bo));
    }

    /**
     * 删除APP用户信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("app:user:remove")
    @Log(title = "APP用户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(appUserService.deleteWithValidByIds(List.of(ids), true));
    }
}
